package app1.rapidsquare.com.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.TimeZone;
import java.util.TreeSet;
import java.util.concurrent.CopyOnWriteArrayList;

import app1.rapidsquare.com.Utility.Utility;
import app1.rapidsquare.com.activity.R;
import app1.rapidsquare.com.classes.Events;

/**
 * Created by Hikmat on 15.08.2014.
 */
public class EventsAdapter extends BaseAdapter {

    private LinkedList<Events> mScheduleList = new LinkedList<Events>();
    private CopyOnWriteArrayList<Events> invalidLines = new CopyOnWriteArrayList<Events>();
    private Picasso mPicasso;
    private LayoutInflater mInflater;
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;
    private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();

    public EventsAdapter(Context context) {
        this.mPicasso = Picasso.with(context);
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public Events checkHeader(Events eventHeader) {
        for (Events event : mScheduleList) {
            if (event.Header.equals(eventHeader.Header))
                return event;
        }
        return null;
    }

    public synchronized void removerHeader(Events eventHeader) {
        LinkedList<Events> toRemove = new LinkedList<Events>();
        for (Events event : mScheduleList) {
            if (event.Header.equals(eventHeader.Header)) {
                toRemove.add(event);
            }
        }

        this.mScheduleList.removeAll(toRemove);
    }

    public void addFirstItem(final Events event) {
        mScheduleList.addFirst(event);
        notifyDataSetChanged();
    }

    public void addFirstSectionHeaderItem(final Events event) {
        mScheduleList.addFirst(event);
        updateSectionHeaderPosition();
    }

    private void updateSectionHeaderPosition() {
        sectionHeader.clear();
        for (Events eventItem : mScheduleList) {
            if (!Utility.isNullOrEmpty(eventItem.Header)) {
                int index = mScheduleList.indexOf(eventItem);
                if (index != -1) {
                    sectionHeader.add(index);
                }
            }
        }
        notifyDataSetChanged();
    }

    public synchronized void replaceEvent(Events event) {
        if (contains(event)) {
            int position = indexOf(event);
            if (position != -1)
                mScheduleList.set(position, event);
            updateSectionHeaderPosition();
        }
    }

    public void addLastItem(final Events event) {
        mScheduleList.addLast(event);
        notifyDataSetChanged();
    }

    public void addLastSectionHeaderItem(final Events event) {
        mScheduleList.addLast(event);
        updateSectionHeaderPosition();
    }

    public void AddPastItems(ArrayList<Events> events) {
        RemovePastEventList(events);
        if (mScheduleList.size() > 0)
            mScheduleList.addAll(0, events);
        else
            mScheduleList.addAll(events);

        notifyDataSetChanged();
    }

    public void AddLastItems(ArrayList<Events> events) {
        events = RemoveLastEventList(events);
        mScheduleList.addAll(events);
        notifyDataSetChanged();
    }

    private void RemovePastEventList(ArrayList<Events> events) {
        LinkedList<Events> toRemove = new LinkedList<Events>();
        for (Events eventItem : events) {
            if (!Utility.isNullOrEmpty(eventItem.Header)) {
                Events event = checkHeader(eventItem);
                if (event != null) {
                    toRemove.add(event);
                }
            }
        }
        this.mScheduleList.removeAll(toRemove);
        notifyDataSetChanged();
    }

    private ArrayList<Events> RemoveLastEventList(ArrayList<Events> events) {
        LinkedList<Events> toRemove = new LinkedList<Events>();
        for (Events eventItem : events) {
            if (!Utility.isNullOrEmpty(eventItem.Header)) {
                Events event = checkHeader(eventItem);
                if (event != null) {
                    toRemove.add(eventItem);
                }
            }
        }
        events.removeAll(toRemove);
        return events;
    }

    @Override
    public int getItemViewType(int position) {
        return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    public LinkedList<Events> getEventsList() {
        return mScheduleList;
    }

    public void setEventsList(LinkedList<Events> events) {
        this.mScheduleList = events;
        updateSectionHeaderPosition();
    }

    public boolean contains(Events event) {
        for (Events item : mScheduleList) {
            if (item.Id == event.Id)
                return true;
        }
        return false;
    }

    public int indexOf(Events event) {
        for (Events item : mScheduleList) {
            if (item.Id == event.Id)
                return mScheduleList.indexOf(item);
        }
        return -1;
    }

    @Override
    public int getCount() {
        return mScheduleList.size();
    }

    @Override
    public Object getItem(int index) {
        if (mScheduleList.size() > index)
            return mScheduleList.get(index);

        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ScheduleViewHolder holder = new ScheduleViewHolder();
        Events currentSchedule = (Events) getItem(position);
        try {
            if (currentSchedule != null
                    && Utility.isNullOrEmpty(currentSchedule.Header)) {

                convertView = mInflater.inflate(R.layout.adapter_event,
                        parent, false);
                holder.Logo = (ImageView) convertView
                        .findViewById(R.id.imageViewLogo);
                holder.Author = (TextView) convertView
                        .findViewById(R.id.textViewEventAuthor);
                holder.Room = (TextView) convertView
                        .findViewById(R.id.textViewEventRoom);
                holder.Time = (TextView) convertView
                        .findViewById(R.id.textViewEventTime);
                holder.Topic = (TextView) convertView
                        .findViewById(R.id.textViewEventTitle);
                holder.LinearLayoutRoom = (LinearLayout) convertView.findViewById(R.id.linearLayoutRoom);
                holder.IsCheck = (ImageView) convertView
                        .findViewById(R.id.imageViewTick);
                holder.Narrow = (ImageView) convertView.findViewById(R.id.imageViewNarrow);

            } else {
                convertView = mInflater.inflate(
                        R.layout.adapter_event_header, null);
                holder.Time = (TextView) convertView
                        .findViewById(R.id.textViewDate);
            }

            if (currentSchedule != null) {
                if (Utility.isNullOrEmpty(currentSchedule.Header)) {

                    holder.Author.setText(currentSchedule.Author);

                    Calendar startDate = Calendar.getInstance();
                    startDate.setTimeZone(TimeZone.getTimeZone("UTC"));
                    startDate.setTime(currentSchedule.StartTime);

                    Calendar endDate = Utility.getNYCDate();
                    endDate.setTimeZone(TimeZone.getTimeZone("UTC"));
                    endDate.setTime(currentSchedule.EndTime);

                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm a");
                    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));

                    holder.Time.setText(simpleDateFormat.format(startDate.getTime()) + " - "
                            + simpleDateFormat.format(endDate.getTime()));


                    holder.Room.setText(currentSchedule.Room);

                    if (Utility.isNullOrEmpty(currentSchedule.Room)) {
                        holder.Logo.setVisibility(View.GONE);
                        holder.Narrow.setVisibility(View.GONE);
                        holder.Author.setVisibility(View.GONE);
                        holder.LinearLayoutRoom.setVisibility(View.GONE);
                    } else {
                        holder.Logo.setVisibility(View.VISIBLE);
                        holder.Narrow.setVisibility(View.VISIBLE);
                        holder.Author.setVisibility(View.VISIBLE);
                        holder.LinearLayoutRoom.setVisibility(View.VISIBLE);
                    }


                    holder.Topic.setText(currentSchedule.Topic);
                    if (currentSchedule.IsCheckIn)
                        holder.IsCheck.setImageResource(R.drawable.tick);
                    else
                        holder.IsCheck.setImageResource(android.R.color.transparent);

                    if (!Utility.isNullOrEmpty(currentSchedule.AuthorImageUrl))
                        LoadPhoto(currentSchedule.AuthorImageUrl, holder.Logo);
                    else
                        holder.Logo.setImageResource(android.R.color.transparent);

                } else {
                    holder.Time.setText(currentSchedule.Header);
                }
            }
        } catch (
                Exception ex
                )

        {
            Utility.Log("Events adapter get view ", ex);
        }

        return convertView;
    }

    private class ScheduleViewHolder {

        public ImageView Logo, IsCheck, Narrow;
        public TextView Time, Topic, Room, Author;
        public LinearLayout LinearLayoutRoom;
    }

    private void LoadPhoto(String imageUrl, ImageView imageView) {
        mPicasso.load(imageUrl)
                .placeholder(R.drawable.agile_icon)
                .error(R.drawable.agile_icon)
                .resizeDimen(R.dimen.image_size,
                        R.dimen.image_size).centerInside().into(imageView);
    }
}
